package com.example.prison.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.DeviceAlarmRecord;
import com.example.prison.service.DeviceAlarmRecordService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Slf4j
@Api(tags = "设备报警记录")
@RestController
@RequestMapping("/api")
public class DeviceAlarmRecordController {

    @Autowired
    private DeviceAlarmRecordService alarmRecordService;

//    @Autowired
//    private SystemLogService systemLogService;

    @ApiOperation("获取设备报警记录列表")
    @GetMapping("/getDeviceAlarmRecord")
    public Response<PageInfo<DeviceAlarmRecord>> getDeviceAlarmRecord(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                      @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                                      @Parameter(description = "开始时间(YYYY-MM-DD HH:MM:SS)")@RequestParam(defaultValue = "0000-00-00 00:00:00") String beginTime,
                                                                      @Parameter(description = "结束时间(YYYY-MM-DD HH:MM:SS)")@RequestParam(defaultValue = "9999-12-31 23:59:59") String endTime,
                                                                      @Parameter(description = "是否处理（未处理/已处理）")@RequestParam(value = "solve", required = false)String solve,
                                                                      @Parameter(description = "报警类型")@RequestParam(value = "type", required = false)String type) {
        PageInfo<DeviceAlarmRecord> pageInfo = alarmRecordService.get(pageNumber, pageSize,beginTime,endTime,solve,type);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("新增设备报警记录")
    @PostMapping("/addDeviceAlarmRecord")
    public Response<?> addDeviceAlarmRecord(@RequestBody DeviceAlarmRecord record) {
        int result = alarmRecordService.add(record);
        log.info("新增设备报警记录"+", 数据："+ JSON.toJSONString(record));
//        systemLogService.addLog("新增设备报警记录");
        return Response.success("添加成功！");
    }

    @ApiOperation("删除设备报警记录")
    @PostMapping("/deleteDeviceAlarmRecord")
    public Response<?> deleteDeviceAlarmRecord(@Parameter(description = "ID") @RequestParam Integer[] ids) {
        Integer[] res = {};
        for(Integer id : ids) {
            if (ObjectUtils.isEmpty(alarmRecordService.getById(id))) {
                res = Arrays.copyOf(res,res.length+1);
                res[res.length-1] = id;
            } else {
                alarmRecordService.delete(ids);
            }
        }
        if(ObjectUtils.isNotEmpty(res)) {
            return Response.info("id为"+ Arrays.toString(res) +"的设备报警记录不存在！",Arrays.toString(res));
        }
        log.info("删除设备报警记录");
//        systemLogService.addLog("删除设备报警记录");
        return Response.success("删除成功！");
    }

    @ApiOperation("处理设备报警记录信息")
    @PostMapping("/handleDeviceAlarmRecord")
    public Response<?> handleDeviceAlarmRecord(@Parameter(description = "ID")@RequestParam Integer id,
                                               @Parameter(description = "处理人")@RequestParam String handle,
                                               @Parameter(description = "处理信息")@RequestParam(defaultValue = "") String handleMessage) {
        alarmRecordService.handle(id,handle,handleMessage);
        log.info("处理报警记录信息"+", 数据："+JSON.toJSONString(alarmRecordService.getById(id)));
//        systemLogService.addLog("处理报警记录信息");
        return Response.success("处理成功！");
    }

    @ApiOperation("依据id获取设备报警记录信息")
    @GetMapping("/getDeviceAlarmRecordById")
    public Response<DeviceAlarmRecord> getDeviceAlarmRecordById(@Parameter(description = "ID")@RequestParam Integer deviceId) {
        DeviceAlarmRecord AlarmRecord = alarmRecordService.getById(deviceId);
        if(ObjectUtils.isEmpty(AlarmRecord)) {
            return Response.error("此id不存在！");
        }
        return Response.success("获取成功！", AlarmRecord);
    }

//    @ApiOperation("依据时间获取设备报警记录信息")
//    @GetMapping("/getDeviceAlarmRecordByTime")
//    public Response<PageInfo<DeviceAlarmRecord>> getDeviceAlarmRecordByTime(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
//                                                                          @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
//                                                                          @Parameter(description = "开始时间(YYYY-MM-DD HH:MM)")@RequestParam(defaultValue = "0000-00-00 00:00") String beginTime,
//                                                                          @Parameter(description = "结束时间(YYYY-MM-DD HH:MM)")@RequestParam(defaultValue = "9999-12-31 23:59") String endTime) {
//        PageInfo<DeviceAlarmRecord> pageInfo = alarmRecordService.getByTime(pageNumber, pageSize, beginTime, endTime);
//        return Response.success("获取成功！", pageInfo);
//    }

}
